package com.ocean.common.event;

import com.ocean.config.GroupDefine;
import com.ocean.constants.ApplicationConstants;
import com.ocean.loader.BlockLoader;
import net.minecraft.block.Block;
import net.minecraft.item.BlockItem;
import net.minecraft.item.Item;
import net.minecraftforge.event.RegistryEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.RegistryObject;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.registries.IForgeRegistry;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * MOD被注册时调用
 *
 * @author zhang
 * @since 2021/11/5
 */
@Mod.EventBusSubscriber(modid = ApplicationConstants.MOD_ID, bus = Mod.EventBusSubscriber.Bus.MOD)
public class RegisterItemsEvent {

    /**
     * 将方块放入分组
     *
     * @param event RegistryEvent
     */
    @SubscribeEvent
    public static void onRegisterItemsEvent(RegistryEvent.Register<Item> event) {
        IForgeRegistry<Item> registry = event.getRegistry();
        List<Block> collect = BlockLoader.BLOCKS.getEntries().stream().map(RegistryObject::get).collect(Collectors.toList());
        for (Block block : collect) {
            Item.Properties properties;
            BlockItem blockItem;
            properties = new Item.Properties().tab(GroupDefine.BLOCK_GROUP);
            blockItem = new BlockItem(block, properties);
            blockItem.setRegistryName(Objects.requireNonNull(block.getRegistryName()));
            registry.register(blockItem);
        }
    }
}
